#include <stdio.h>
#include <string.h>

#include "main.h"
#include "gpio.h"
#include "RCC\bsp_rcc.h"
#include "KEY_LED\bsp_key_led.h"
#include "LCD\bsp_lcd.h"
#include "UART\bsp_uart.h"

typedef unsigned char uint8_t;



//????????????
__IO uint32_t uwTick_Key_Set_Point = 0;
__IO uint32_t uwTick_Led_Set_Point = 0;
__IO uint32_t uwTick_Lcd_Set_Point = 0;
__IO uint32_t uwTick_Usart_Set_Point = 0;

//?????????????
uint8_t ucKey_Val , unKey_Down , ucKey_Up , ucKey_Old;

//LED?????????
uint8_t ucLed;

//USART
int counter = 0;
char str[40];


//???????????
void Key_Proc(void);
void Led_Proc(void);
void Lcd_Proc(void);
void Usart_Proc(void);




// System main function
int main(void)
{
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* Configure the system clock */
    SystemClock_Config();

    KEY_LED_Init();
    
    LED_Disp(ucLed);
    
    KEY_Scan();
    
    LCD_Init();
    LCD_Clear(White);
    LCD_SetBackColor(White);
    LCD_SetTextColor(Black);
    LCD_DisplayStringLine(Line0, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line1, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line2, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line3, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line4, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line5, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line6, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line7, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line8, (u8 *)"Hello World!");
    LCD_DisplayStringLine(Line9, (u8 *)"Hello World!");

		UART1_Init();





    while (1)
    {
        Key_Proc();  // Correct function call
        Led_Proc();  // Correct function call
        Lcd_Proc();  // Correct function call
				Usart_Proc();
    }
}

//????????????

void Key_Proc(void)
{
    if((uwTick - uwTick_Key_Set_Point) < 20) return;
    uwTick_Key_Set_Point = uwTick;
    
    ucKey_Val = KEY_Scan();
    unKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);
    ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);  // Fixed: use bitwise NOT instead of negative
    ucKey_Old = ucKey_Val;
    
    if(unKey_Down == 4)
    {
        LED_Disp(0x88);
        ucLed = 0x88;  // Update ucLed variable
    }
    if(unKey_Down == 3)
    {
        LED_Disp(0x00);
				 ucLed = 0x00;
		}
}


//LED????????

void Led_Proc(void)
{
    if((uwTick - uwTick_Led_Set_Point) < 20) return;
    uwTick_Led_Set_Point = uwTick;
    
    LED_Disp(ucLed);
}

void Lcd_Proc(void)
{
    if((uwTick - uwTick_Lcd_Set_Point) < 20) return;
    uwTick_Lcd_Set_Point = uwTick;
    
}


void Usart_Proc(void)
{
    if((uwTick - uwTick_Usart_Set_Point) < 100) return;
    uwTick_Usart_Set_Point = uwTick;
    
	sprintf(str, "%04d:Hello,world.\n", counter);
	HAL_UART_Transmit(&huart1,(unsigned char *)str, strlen(str), 50);
	
	if(++counter == 10000)
		counter = 0;
		
}























void Error_Handler(void)
{
    __disable_irq();
    while (1)
    {
        // Stay in error state
    }
}




















































